//use "./Data/RatesReturns/Basis_and_Returns.dta", clear
* use "./Data/Extension2020/Basis_and_Returns.dta", clear
//use "./Data/Extension2021/Basis_and_Returns.dta", clear
 
 use "./OutputInterim/Basis_and_Returns.dta", clear
 
local currencies JPY AUD EUR CAD GBP CHF

local workingdays 257

format Date %td
gen post_crisis = Date >= mdy(7,1,2010)


gen fctr_ois_log_return_1M_fwd_3M = (AUD_USD_ois_r_1M_fwd_3M - JPY_USD_ois_r_1M_fwd_3M)

gen fctr_ois_log_return_3M_fwd_3M = (AUD_USD_ois_r_3M_fwd_3M - JPY_USD_ois_r_3M_fwd_3M)

gen fctr_ois_spot_log_basis_3M = AUD_USD_ois_spot_log_basis_3M - JPY_USD_ois_spot_log_basis_3M 

label var fctr_ois_spot_log_basis_3M "Spot Basis 3m"

foreach ccy in `currencies' {

	gen `ccy'_spread1M = `ccy'_USD_ois_log_basis_1M_fwd_3M - `ccy'_USD_ois_spot_log_basis_3M
	gen `ccy'_spread3M = `ccy'_USD_ois_log_basis_3M_fwd_3M - `ccy'_USD_ois_spot_log_basis_3M
	
}

gen fctr_spread1M = AUD_spread1M - JPY_spread1M
gen fctr_spread3M = AUD_spread3M - JPY_spread3M

label var fctr_spread1M "1mFwd3m-3m Spread"
label var fctr_spread3M "3mFwd3m-3m Spread"

gen pre_ye = 1 //month(Date) < 9

sort Date

bcal create "./OutputInterim/spotdates", from(Date) replace
adopath + "./OutputInterim/"
	
gen bdate = bofd("spotdates",Date) 


	
tsset bdate

local ccy fctr

gen cons = 1

tempfile temp
save `temp', replace

foreach precov in N Y {

foreach fwd in 1M { //3M 

	use `temp', clear
	
	if "`precov'"=="Y" {
		//this is the last trade ending by the end of the year for 1M
		//we don't actually use the 3M forward in the paper
		replace pre_ye = Date <= mdy(11,30,2019)
	}
	
	//keep fctr_ois_log_return_1M_fwd_3M fctr_ois_log_return_3M_fwd_3M fctr_spread1M fctr_spread3M fctr_ois_spot_log_basis_3M Date post_crisis pre_ye bdate cons
	
	local n = 12
	
	if "`fwd'" == "3M" {
		local n = 4
	}

	// first, do a specification check
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M cons if post_crisis & pre_ye & `ccy'_spread`fwd' != ., robust bw(auto) noconstant
	
	local aic1 = log(`e(rmse)') + 2/(`e(N)'*`n'/`workingdays')
	
	eststo consonly
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_spread`fwd' if post_crisis & pre_ye, robust bw(auto) noconstant
	
	local aic2 = log(`e(rmse)') + 2/(`e(N)'*`n'/`workingdays')
	eststo spreadonly
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_spread`fwd' if post_crisis & pre_ye, robust bw(auto)
	
	predict basis_shock, residual
	
	local aic3 = log(`e(rmse)') + 4/(`e(N)'*`n'/`workingdays')
	eststo spreadcons
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_spread`fwd' `ccy'_ois_spot_log_basis_3M if post_crisis & pre_ye, robust bw(auto) noconstant
	local aic4 = log(`e(rmse)') + 4/(`e(N)'*`n'/`workingdays')
	eststo spreadspot
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_spread`fwd' `ccy'_ois_spot_log_basis_3M if post_crisis & pre_ye, robust bw(auto)
	local aic5 = log(`e(rmse)') + 6/(`e(N)'*`n'/`workingdays')
	eststo spreadconsspot
	
	
	
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_ois_spot_log_basis_3M (`ccy'_spread`fwd'=L1.`ccy'_spread`fwd') if post_crisis & pre_ye, robust bw(auto) first noconstant
	
	eststo lag1
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_ois_spot_log_basis_3M (`ccy'_spread`fwd'=L5.`ccy'_spread`fwd') if post_crisis & pre_ye, robust bw(auto) first noconstant
	
	eststo lag5
	
	ivreg2 `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_ois_spot_log_basis_3M (`ccy'_spread`fwd'=L10.`ccy'_spread`fwd') if post_crisis & pre_ye, robust bw(auto) first noconstant
	
	eststo lag10
	
	esttab consonly spreadonly spreadcons spreadspot spreadconsspot lag1 lag5 lag10, eqlabels(none) se scalars(rmse r2u bw widstat exexog) rename(cons _cons) obslast nomtitles label star(* 0.1 ** 0.05 *** 0.01)
	
	esttab consonly spreadonly spreadcons spreadspot spreadconsspot lag1 lag5 lag10 using "./OutputFinal/return_pred`fwd'_`precov'.csv", eqlabels(none) se scalars(rmse r2u bw widstat exexog) rename(cons _cons) obslast nomtitles label csv replace star(* 0.1 ** 0.05 *** 0.01)
	
	esttab consonly spreadonly spreadcons spreadspot spreadconsspot lag1 lag5 lag10 using "./OutputFinal/return_pred`fwd'_`precov'.tex", eqlabels(none) se scalars(rmse r2u bw widstat exexog) rename(cons _cons) obslast nomtitles label tex replace star(* 0.1 ** 0.05 *** 0.01)
	
	disp "aics: `aic1' `aic2' `aic3' `aic4' `aic5'" 
	

	//reg `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_ois_spot_log_basis_3M `ccy'_spread`fwd' if post_crisis, noconstant
	
	
	if "`precov'"=="Y" {
		su `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_ois_spot_log_basis_3M `ccy'_spread`fwd' if post_crisis & pre_ye
		su `ccy'_ois_log_return_`fwd'_fwd_3M `ccy'_ois_spot_log_basis_3M `ccy'_spread`fwd' if post_crisis & ~pre_ye
	}
	
	rename fctr_ois_spot_log_basis_3M spot_basis
	rename fctr_ois_log_return_1M_fwd_3M basis_return
	rename fctr_spread`fwd' basis_spread
	gen lag_basis_spread = L1.basis_spread
	
	keep Date basis_shock basis_return spot_basis basis_spread lag_basis_spread post_crisis pre_ye
	gen month = mofd(Date)
	sort Date
	
	//last trading day in june is really the july monthly return
	replace post_crisis = Date >= mdy(6,30,2010)
	collapse (lastnm) basis_shock basis_return spot_basis basis_spread lag_basis_spread post_crisis pre_ye, by(month)
	
	rename month Date
	format Date %tm
	sort Date
	
	label var spot_basis "Spot Basis 3m"
	label var basis_spread "Basis Spread `fwd'-3m"
	
	reg basis_return if post_crisis &  pre_ye & basis_spread != ., vce(robust)
	
	eststo consonly
	
	reg basis_return basis_spread if post_crisis & pre_ye, vce(robust) noconstant
	
	eststo spreadonly
	
	reg basis_return basis_spread if post_crisis & pre_ye, vce(robust) 
	
	eststo spreadcons
	
	reg basis_return basis_spread spot_basis if post_crisis & pre_ye, vce(robust) noconstant
	eststo spreadspot
	
	reg basis_return basis_spread spot_basis if post_crisis & pre_ye, vce(robust)

	eststo spreadconsspot
	
	
	ivreg2 basis_return spot_basis (basis_spread=lag_basis_spread) if post_crisis & pre_ye, robust first noconstant
	
	eststo lag1
	
	ivreg2 basis_return (basis_spread=lag_basis_spread) if post_crisis & pre_ye, robust first noconstant
	
	eststo lag1so
	
	
	//cqiv basis_return (basis_spread=lag_basis_spread) if post_crisis & pre_ye, uncensored firststage(ols) confidence(boot)
	ivreg2 basis_return (basis_spread=lag_basis_spread) if post_crisis & pre_ye, robust first
	
	eststo lag1soc
	
	esttab consonly spreadonly spreadcons spreadspot spreadconsspot lag1so lag1soc lag1, eqlabels(none) se scalars(rmse r2u widstat exexog) rename(cons _cons) obslast nomtitles label star(* 0.1 ** 0.05 *** 0.01)
	
	esttab consonly spreadonly spreadcons spreadspot spreadconsspot lag1so lag1soc lag1 using "./OutputFinal/return_pred`fwd'_monthly_`precov'.csv", eqlabels(none) se scalars(rmse r2u widstat exexog) rename(cons _cons) obslast nomtitles label csv replace star(* 0.1 ** 0.05 *** 0.01)
	
	esttab consonly spreadonly spreadcons spreadspot spreadconsspot lag1so lag1soc lag1 using "./OutputFinal/return_pred`fwd'_monthly_`precov'.tex", eqlabels(none) se scalars(rmse r2u widstat exexog) rename(cons _cons) obslast nomtitles label tex replace star(* 0.1 ** 0.05 *** 0.01)
	
	drop basis_spread lag_basis_spread post_crisis
	
	save "./OutputInterim/BasisShock`fwd'_`precov'.dta", replace
}
}

